﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Jemgine.MapEditor.Operations
{
	internal class MakeGear : Operation
	{
        Engine.Selection Selected;
        Engine.Selection ItemAdded;
		CreateGearProperties _settings;
        Data.EntityRecord Entity;

        internal MakeGear(Engine.Selection Selected, CreateGearProperties _settings)
		{
			this.Selected = Selected;
			this._settings = _settings;
		}

		internal override ReturnCode Do(EditorState ES)
		{
            Entity = ES.GetTopObject(ES.Selected) as Data.EntityRecord;
            if (Entity == null) return ReturnCode.DoNotAddToUndoStack;

		
			        var Points = GearFactory.CreateGear(
			            _settings.Radius,
			            _settings.Teeth,
			            _settings.TipPercentage,
			            _settings.ToothDepth);
			        Points.Reverse();

			
			Entity.AttachedElements.Add(Points);

            ItemAdded = new Engine.Selection(Engine.Selection.Types.EntityElement, Selected.TopIndex,
				Entity.AttachedElements.Count - 1, 0);
            ES.RebuildNode(ItemAdded.TopIndex);

			ES.SelectItem(ItemAdded);

			return ReturnCode.AddToUndoStack;
		}

		internal override void Undo(EditorState ES)
		{
            Entity.AttachedElements.RemoveAt(ItemAdded.MiddleIndex);
            ES.RebuildNode(ItemAdded.TopIndex);

            ES.SelectItem(new Jemgine.Engine.Selection(Jemgine.Engine.Selection.Types.MapElement, ItemAdded.TopIndex, 0, 0));
		}
	}
}
